/**  
* Copyright (c) 2008, Noel Lynch All rights reserved. Redistribution and use  
* in source and binary forms, with or without modification, are permitted  
* provided that the following conditions are met: Redistributions of source  
* code must retain the above copyright notice, this list of conditions and the  
* following disclaimer. Redistributions in binary form must reproduce the above  
* copyright notice, this list of conditions and the following disclaimer in the  
* documentation and/or other materials provided with the distribution. Neither  
* the name of Noel Lynch nor the names of contributors may be used to  
* endorse or promote products derived from this software without specific prior  
* written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT  
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A  
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR  
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;  
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR  
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF  
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  
*/
// Apr 24, 2009

package com.sceneworker.util.swing.button;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JComponent;

import com.myjavautils.swing.DrawUtils;

public class SimpleImageButton extends JComponent implements MouseListener {

	private		String				mstr_imLocation;
	private		BufferedImage		mcl_image;
	
	private		IClickListener		mcl_clickListener;
	
	public SimpleImageButton(String str_imageLocation) {
		try {
			setName(str_imageLocation);
			mcl_image = ImageIO.read(SimpleImageButton.class.getClassLoader().getResource(str_imageLocation));
			addMouseListener(this);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public	void	setClickListener(IClickListener cl_l) {
		mcl_clickListener = cl_l;
	}
	
	@Override
	public Dimension getPreferredSize() {
		return new Dimension(mcl_image.getWidth(), mcl_image.getHeight());
	}
	
	@Override
	public Dimension getMaximumSize() {
		return new Dimension(mcl_image.getWidth(), mcl_image.getHeight());
	}

	@Override
	public Dimension getMinimumSize() {
		return new Dimension(mcl_image.getWidth(), mcl_image.getHeight());
	}
	
	@Override
	protected void paintComponent(Graphics cl_g) {
		super.paintComponent(cl_g);
		Graphics2D			lcl_g = (Graphics2D)cl_g.create();
		
		lcl_g.drawImage(mcl_image, 0, 0, this);
		DrawUtils.drawComponentRect(lcl_g, this);
		
		lcl_g.dispose();
	}

	public void mouseClicked(MouseEvent e) {
		if(mcl_clickListener != null) {
			mcl_clickListener.handleClick(this);
		}
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mousePressed(MouseEvent e) {
	}

	public void mouseReleased(MouseEvent e) {
	}
	
	public	interface	IClickListener {
		void	handleClick(SimpleImageButton cl_src);
	}

}
